define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'company/index' + location.search,
                    add_url: 'company/add',
                    edit_url: 'company/edit',
                    del_url: 'company/del',
                    multi_url: 'company/multi',
                    dragsort_url: 'ajax/weigh',
                    table: 'company',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                sortOrder: 'desc',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'name', title: __('公司名称'), operate: 'LIKE' },
                        { field: 'logo', title: __('Logo'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image },
                        { field: 'weigh', title: __('权重'), operate: false },
                        { field: 'createtime', title: __('创建时间'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime },
                        { field: 'updatetime', title: __('更新时间'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime },
                        { field: 'status', title: __('状态'), searchList: { "normal": __('Normal'), "hidden": __('Hidden') }, formatter: Table.api.formatter.status },
                        { field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            // 绑定拖动排序
            require(['dragsort'], function () {
                //绑定拖动排序
                $("tbody").dragsort({
                    itemSelector: 'tr',
                    dragSelector: "tr",
                    dragEnd: function () {
                        var data = table.bootstrapTable('getData');
                        var ids = [];
                        $.each(data, function (i, j) {
                            ids.push(j.id);
                        });
                        $("input[name=weigh]").val(ids.join(','));
                        table.bootstrapTable('refresh');
                    },
                    placeHolderTemplate: ""
                });
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
}); 